package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public PageResult selectPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page page=categoryMapper.selectPage(categoryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 更新分类信息
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {

        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
    }

    @Override
    public void startOrStopCategory(Long id, Integer status) {
        Category category=new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> selectCategoryByType(Integer type) {
        return categoryMapper.selectCategoryByType(type);
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //默认新添加的分类暂时为禁用状态
        category.setStatus(StatusConstant.DISABLE);

        //设置创建时间、更新时间、创建者、更新者
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper层进行添加
        categoryMapper.insert(category);
    }

    @Override
    public void deleteCategoryById(Long id) {
        //如果dish表中有关联该类，则不允许删除
        if(dishMapper.countByCategoryId(id)>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //如果setmeal表内有关联该类，则不i允许删除
        if (setmealMapper.countByCategoryId(id)>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> selectCategories() {
        return categoryMapper.selectCategories();
    }
}
